package cn.itsub.proxy.server.app.controller;


import cn.itsub.proxy.common.model.Metrics;
import cn.itsub.proxy.server.app.entity.Tunnel;
import cn.itsub.proxy.common.utils.Result;
import cn.itsub.proxy.server.app.service.TunnelService;
import cn.itsub.proxy.server.app.utils.NetUtil;
import cn.itsub.proxy.server.work.config.ProxyConfig;
import cn.itsub.proxy.server.work.metrics.MetricsCollector;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import lombok.var;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Java夏老师
 * @since 2021-09-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    public static final String MIME_JSON = "application/json;charset=UTF-8";

    @Autowired TunnelService tunnelService;

    @RequestMapping("/demo")
    @ResponseBody
    public String demo() {

        return "HelloWorld";
    }



    @ResponseBody
    @RequestMapping(value="/tunnel/add",produces = MIME_JSON)
    public Object add(Tunnel tunnel,HttpSession session) {
        //数据校验
        if (!NetUtil.ipCheck(tunnel.getLanHost())) {
            return result(91, "LanHost不是有效的IP地址");
        }
        if (tunnel.getWanPort()<10000||tunnel.getWanPort()>60000) {
            return result(92, "外网端口超出范围");
        }
        if (tunnel.getLanPort()<0||tunnel.getLanPort()>65535) {
            return result(92, "内网端口超出范围");
        }
        //是否已经登录
        String user = (String) session.getAttribute("login");
        if (user == null) {
            return result(100, "未登录");
        }
        //保存数据
        tunnel.setState(0);
        tunnelService.frpReloadTunnels(tunnel.getClientKey());
        return Result.of(200,"添加成功");
    }

    @ResponseBody
    @RequestMapping(value="/tunnel/update",produces = MIME_JSON)
    public Object tunnelUpdate(Tunnel tunnel,HttpSession session) {
        return tunnelService.updateTunnel(tunnel);
    }

    @RequestMapping(value = "/tunnel/list",produces=MIME_JSON)
    @ResponseBody
    public Object tunnelList(HttpSession session) {

        List<Tunnel> list = null;

        Gson gson = new Gson();
        JsonObject json = new JsonObject();
        json.addProperty("code", 0);
        json.addProperty("msg", "获取成功");
        json.add("data",gson.toJsonTree(list));
        return json.toString();
    }


    //修改隧道的启用状态，0未启用，1已启用
    @RequestMapping(value="/tunnel/state/turn",produces="application/json;charset=UTF-8")
    @ResponseBody
    public Object tunnelTurnState(int tunnelId,int state, HttpSession session) {
        String user = (String) session.getAttribute("login");
        if (user == null) {
            return result(100, "未登录");
        }
        //todo
        /*var tunnel = dao.getTunnelById(tunnelId);
        if (tunnel.getState() != state) {
            tunnel.setState(state);
            storage.save();
            tunnelService.frpReloadTunnels(tunnel.getClientKey());
        }*/
        JsonObject json = new JsonObject();
        json.addProperty("code", 0);
        json.addProperty("msg", "执行完成");
        return json.toString();
    }



    //查询统计信息
    @RequestMapping(value = "/metrics",produces=MIME_JSON)
    @ResponseBody
    public Object metrics(int[] port,HttpSession session) {
        String user = (String) session.getAttribute("login");
        if (user == null) {
            return result(100, "未登录");
        }

        List<Metrics> list = new ArrayList<>();
        if (port != null) {
            for (int p : port) {
                MetricsCollector mc = MetricsCollector.getCollector(p);
                list.add(mc.getMetrics());
            }
        }

        Gson gson = new Gson();
        JsonObject json = new JsonObject();
        json.addProperty("code", 0);
        json.addProperty("msg", "成功");
        json.add("data",gson.toJsonTree(list));
        return json.toString();
    }



    private String result(int code, String msg) {
        JsonObject json = new JsonObject();
        json.addProperty("code", code);
        json.addProperty("msg", msg);
        return json.toString();
    }

}

